package net.zjitc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.zjitc.pojo.*;
import net.zjitc.service.NoticeService;
import net.zjitc.service.OrdersService;
import net.zjitc.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/15 8:37
 */
@Controller
@RequestMapping("/products")
public class ClientProductsController {
    @Autowired
    ProductsService productsService;
    @Autowired
    NoticeService noticeService;
    @Autowired
    OrdersService ordersService;

    @RequestMapping("/index")
    public String index(Model model){
        List<Category> categoryList = productsService.findAllCategory();
        Notice notice = noticeService.descNoticeDate();
        List<OrderItem> newThreeProducts = ordersService.findNewThreeProducts();
        ArrayList<Object> list = new ArrayList<>();
        for (int i=0;i<3;i++){
            Products newProducts = productsService.findNewProducts(newThreeProducts.get(i).getProduct_id());
            list.add(newProducts);
        }
        model.addAttribute("cList",categoryList);
        model.addAttribute("notice",notice);
        model.addAttribute("newThreeProducts",newThreeProducts);
        model.addAttribute("pList",list);
        return "/client/index.jsp";
    }

    //没用了、。。。。。。。。。。。。。。。。。。。。。。被pageProducts（。。。）取代了
    //_________________________________________________________________________________
//    @RequestMapping("/showProductByPage")
//    public String showProductByPage(@RequestParam(required = false) Integer id, Model model){
//        //导航分类
//        List<Category> categoryList = productsService.findAllCategory();
//        model.addAttribute("cList",categoryList);
//        //查找当前分类名字
//        Category category = productsService.findCategoryById(id);
//        model.addAttribute("category",category);
//        //查找当前分类下的商品
//        List<Products> productsList = productsService.findProductByCid(id);
//        model.addAttribute("pList",productsList);
//        //查找商品总数量
//        int count = productsList.size();
//        model.addAttribute("count",count);
//
//        return "/client/product_list.jsp";
//    }


    //没用了、。。。。。。。。。。。。。。。。。。。。。。被pageSearchServlet（。。。）取代了
    //_________________________________________________________________________________
//    @RequestMapping("/menuSearchServlet")
//    public String menuSearchServlet(Condition condition,Model model){
//        //导航分类
//        List<Category> categoryList = productsService.findAllCategory();
//        model.addAttribute("cList",categoryList);
//        //查找当前分类名字
//        Category category = new Category();
//        category.setName("搜索");
//        model.addAttribute("category",category);
//        //查找当前分类下的商品
//        List<Products> productsList = productsService.searchByCondition(condition);
//        model.addAttribute("pList",productsList);
//        //查找商品总数量
//        int count = productsList.size();
//        model.addAttribute("count",count);
//
//        return "/client/product_list.jsp";
//    }

    @RequestMapping("/findProductById")
    public String findProductById(String id,Model model){
        Products product = productsService.findProductById(id);
        model.addAttribute("p",product);
        return "/client/info.jsp";
    }

    /*查找分页*/
    @RequestMapping("/pageProducts")
    public String pageProducts(Integer currentPage,
                               @RequestParam(required = false) Integer id,
                               Model model){

        //导航分类
        List<Category> categoryList = productsService.findAllCategory();
        model.addAttribute("cList",categoryList);

        //查找当前分类名字
        Category category = productsService.findCategoryById(id);
        model.addAttribute("category",category);

        //当前页的信息
        PageHelper.startPage(currentPage,6);
        List<Products> list = productsService.findProductByCid(id);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        return "/client/product_Pagelist.jsp";
    }

    /*搜索分页*/
    @RequestMapping("/pageSearchServlet")
    public String pageSearchServlet(Integer currentPage,
                                    Condition condition,
                                    Model model){
        //导航分类
        List<Category> categoryList = productsService.findAllCategory();
        model.addAttribute("cList",categoryList);

        //查找当前分类名字
        Category category = new Category();
        category.setName("搜索");
        model.addAttribute("category",category);

        //放入查找关键字
        String name = condition.getName();
        model.addAttribute("name",name);
        //当前页的信息
        PageHelper.startPage(currentPage,6);
        List<Products> list = productsService.searchByCondition(condition);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        return "/client/product_search_Pagelist.jsp";
    }
}
